﻿using System.Collections;
using UnityEngine;
using UnityEngine.Networking;
using System.IO;
using System;
using ZXing;
using ZXing.QrCode;
using UnityEngine.UI;

public class FileUpload : MonoBehaviour
{
    public Image Zxing;
    private Texture2D encoded;
    private string filename;
    private string urlpath;
    private string filepath;
    private int result;
    //图片转base64string
    public string Texture2dToBase64(string texture2d_path)
    {
        //将图片文件转为流文件
        FileStream fs = new System.IO.FileStream(texture2d_path, System.IO.FileMode.Open, System.IO.FileAccess.Read);
        byte[] thebytes = new byte[fs.Length];
        fs.Read(thebytes, 0, (int)fs.Length);
        //转为base64string
        string base64_texture2d = Convert.ToBase64String(thebytes);
        return base64_texture2d;
    }
    IEnumerator UploadFile()
    {

        print("开始上传文件：" + filename);
        WWWForm form = new WWWForm();
        form.AddField("filename", filename);
        form.AddField("urlpath", urlpath);

        form.AddField("data", Texture2dToBase64(filepath));
        //WWW www = new WWW("www.你的域名.com/upload.php", form);
        UnityWebRequest request = UnityWebRequest.Post ("www.你的域名.com/upload.php", form);
        //yield return www;
        yield return request;

        //if (www.text.Trim() == "0000")
        //{
        //    result = 1;
        //    print("上传成功");
        //}
        //else
        //{
        //    print(www.text);
        //    result = 0;
        //    //print("上传失败");
        //}
        if (request.ToString().Trim() == "0000")
        {
            result = 1;
            print("上传成功");
        }
        else
        {
            print(request.ToString());
            result = 0;
            print("上传失败");
        }

    }
    /// <summary>
    /// 返回指定URL的二维码
    /// </summary>
    /// <param name="str">url,比如http://www.xxx.com/showpic.php?filename=1001.jpg</param>
    /// <returns></returns>
    public Texture2D ShowCode(string str)
    {
        encoded = new Texture2D(256, 256);
        //QRCodes = str;
        var textForEncoding = str;
        if (textForEncoding != null)
        {
            //二维码写入图片
            var color32 = Encode1(textForEncoding, encoded.width, encoded.height);
            encoded.SetPixels32(color32);
            encoded.Apply();
            Zxing.sprite = GetSpriteByTexture(encoded);
            return encoded;
        }
        return null;
    }

    //定义方法生成二维码
    private static Color32[] Encode1(string textForEncoding, int width, int height)
    {
        var writer = new BarcodeWriter
        {
            Format = BarcodeFormat.QR_CODE,
            Options = new QrCodeEncodingOptions
            {
                Height = height,
                Width = width
            }
        };
        return writer.Write(textForEncoding);
    }
    Sprite GetSpriteByTexture(Texture2D tex)
    {
        Sprite _sprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(0.5f, 0.5f));
        return _sprite;
    }
    public void ZxingButton()
    {
        ShowCode("https://gitee.com/9624/zxing.-net.-x/blob/master/Icons/logo.jpg");
    }
}